/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AppServices;

import Facades.PlanFacadeRemote;
import Facades.UsersFacadeRemote;
import VOs.Plan;
import VOs.Users;
import VOs.Reserve;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.naming.InitialContext;

/**
 *
 * @author Camilo
 */
public class PlanManage {

    public static PlanFacadeRemote getStartContext() throws Exception {
        InitialContext ctx = new InitialContext();
        PlanFacadeRemote currentBean = (PlanFacadeRemote) ctx.lookup("Facades.PlanFacadeRemote");
        return currentBean;
    }

    public static UsersFacadeRemote getUserStartContext() throws Exception {
        InitialContext ctx = new InitialContext();
        UsersFacadeRemote currentBean = (UsersFacadeRemote) ctx.lookup("Facades.UsersFacadeRemote");
        return currentBean;
    }

    public static Plan createPlan2(String userName) {
        Plan currentPlan = null, currentScanPlan = null;
        ;
        Users referenceUser = null;
        List<Plan> currentFullList = null;
        int i;
        BigDecimal maxId = BigDecimal.ZERO;

        try {
            referenceUser = getUserStartContext().findid(userName);

            if (referenceUser == null) {
                return null;
            }

            currentFullList = getStartContext().findAll();

            for (i = 0; i < currentFullList.size(); i++) {
                currentScanPlan = currentFullList.get(i);

                if (i == 0) {
                    maxId = currentScanPlan.getIdPlan();
                } else {
                    if ((maxId.compareTo(currentScanPlan.getIdPlan())) == -1) {
                        maxId = currentScanPlan.getIdPlan();
                    }
                }
            }

            maxId = maxId.add(BigDecimal.ONE);

            currentPlan = new Plan();
            currentPlan.setFkUsername(referenceUser);
            currentPlan.setIdPlan(maxId);

            getStartContext().create(currentPlan);
        } catch (Exception e) {
            return null;
        }

        return currentPlan;
    }

    public static boolean createPlan(String userName) {
        Plan currentPlan = null, currentScanPlan = null;
        ;
        Users referenceUser = null;
        List<Plan> currentFullList = null;
        int i;
        BigDecimal maxId = BigDecimal.ZERO;

        try {
            referenceUser = getUserStartContext().findid(userName);

            if (referenceUser == null) {
                return false;
            }

            currentFullList = getStartContext().findAll();

            for (i = 0; i < currentFullList.size(); i++) {
                currentScanPlan = currentFullList.get(i);

                if (i == 0) {
                    maxId = currentScanPlan.getIdPlan();
                } else {
                    if ((maxId.compareTo(currentScanPlan.getIdPlan())) == -1) {
                        maxId = currentScanPlan.getIdPlan();
                    }
                }
            }

            maxId = maxId.add(BigDecimal.ONE);

            currentPlan = new Plan();
            currentPlan.setFkUsername(referenceUser);
            currentPlan.setIdPlan(maxId);

            getStartContext().create(currentPlan);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public static List<Plan> getByUsername(String userName, int pageCode, int pageLength) {
        List<Plan> dev = null, fullList = null;
        int startData = (pageCode - 1) * pageLength;
        int currentData = 1;
        Plan currentPlan = null;

        try {
            fullList = getStartContext().findAll();
        } catch (Exception e) {
            return null;
        }

        dev = new LinkedList<Plan>();

        for (int i = 0; i < fullList.size(); i++) {
            currentPlan = fullList.get(i);

            if (currentPlan.getFkUsername().getName().trim().toLowerCase().equals(userName.trim().toLowerCase())) {
                if (pageLength == 0) {
                    dev.add(currentPlan);
                } else {
                    if ((currentData >= startData) && (startData <= (startData + pageLength))) {
                        dev.add(currentPlan);
                    }

                    if (startData > (startData + pageLength)) {
                        break;
                    }

                    currentData++;
                }
            }

        }

        return dev;
    }

    public static List<Plan> getPlansWithActiveReservesByUsername(String userName) throws Exception {
        List<Plan> dev = null, fullList = null;
        Plan currentPlan = null;
        Collection<Reserve> currentCollectionReserve;
        Reserve currentReserve;
        boolean currentActiveReserves;
        Iterator<Reserve> iteratorReserveCollection;
        BigInteger activeReserveCode = BigInteger.ONE;

        try {
            fullList = getStartContext().findAll();
        } catch (Exception e) {
            return null;
        }

        dev = new LinkedList<Plan>();

        for (int i = 0; i < fullList.size(); i++) {
            currentPlan = fullList.get(i);
            currentActiveReserves = false;

            if (currentPlan.getFkUsername().getUsername().equals(userName)) {

                currentCollectionReserve = currentPlan.getReserveCollection();

                if (currentCollectionReserve != null) {
                    iteratorReserveCollection = currentCollectionReserve.iterator();

                    while (iteratorReserveCollection.hasNext()) {
                        currentReserve = iteratorReserveCollection.next();

                        if (currentReserve.getActivestate().equals(activeReserveCode)) {
                            currentActiveReserves = true;
                            break;
                        }
                    }
                }

                if (currentActiveReserves) {
                    dev.add(currentPlan);
                }

            }
        }

        return dev;
    }

    static public List<Plan> getall(String user) throws Exception {
        List<Plan> temp = new ArrayList<Plan>();
        List<Plan> planes = getStartContext().findAll();
        for (int i = 0; i < planes.size(); i++) {
            if (planes.get(i).getFkUsername().getUsername().equals(user)) {
                temp.add(planes.get(i));
            }
        }
        return temp;
    }
}
